<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/2 0002
 * Time: 11:03
 */

namespace app\admin\controller;

use think\Controller;
use app\home\model\BfNewslanmu;

class Newslanmu extends Controller{
    
    public function initialize(){
    }
    
    /*=======================新闻分类===========================*/
    public function index(){
        
        $list = BfNewslanmu :: select();
        
        
        foreach($list as $v0){
            if($v0['topid'] == '0'){
                $v0['padding'] = '20';
                $listarr[] = $v0;
                foreach($list as $v1){
                    if($v1['topid'] == $v0['id']){
                        $v1['padding'] = '60';
                        $listarr[] = $v1;
                    }
                }
            }
        }
        
        $this -> assign('list',$listarr);
        return $this->fetch();
    }
    
    public function getList(){
        $search = [
            'pagesize' => '20',
        ];
        $list = BfNewslanmu :: loadlist($search);
        $this -> result($list,0,'','json');
    }
    /**
     * 添加
     * */
    public function add(){
        $method = $this -> request -> method();
        $get = $this -> request ->get();
        if($method === 'GET'){
            return $this->fetch();
        }else{
            $post = $this -> request -> post();
            $data = [
                'topid' => $post['topid'],
                'title'=>$post['title'],
                'keyword'=>$post['keyword'],
                'description'=>$post['description'],
                'name'=>$post['name'],
                'pname' => $post['pname'],
                'content'=>$post['editorValue'],
                'lietemp' => $post['lietemp'],
                'neitemp' => $post['neitemp'],
                'create_time'=>time(),
            ];
            $id = BfNewslanmu :: create($data);
            if($id){
                $this -> result($id,200,'添加成功','json');
            }else{
                $this -> result($id,0,'添加失败','json');
            }
        }
    }
    /**
     * 修改
     * */
    public function edit(){
        $method = $this -> request -> method();
        $get = $this -> request ->get();
        if($method === 'GET'){
            $data = BfNewslanmu :: get($get['id']);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $post = $this -> request -> post();
            $data = [
                'title'=>$post['title'],
                'keyword'=>$post['keyword'],
                'description'=>$post['description'],
                'name'=>$post['name'],
                'pname' => $post['pname'],
                'content'=>$post['editorValue'],
                'lietemp' => $post['lietemp'],
                'neitemp' => $post['neitemp'],
            ];
            
            $id = BfNewslanmu :: update($data,['id'=>$post['id']]);
            if($id){
                $this -> result($id,200,'修改成功','json');
            }else{
                $this -> result($id,0,'修改失败','json');
            }
        }
    }
}